import { useRequest } from "ahooks";
import { useSearchParams } from "react-router-dom";
import { getQuestionListService } from "../service/questions";
import { LIST_SEARCH_PARAM_KEY, LIST_PAGE_PARAM_KEY, LIST_PAGE_SIZE_PARAM_KEY, LIST_PAGE_SIZE } from "../constant";

type OptionType = {
  isStar: boolean,
  isDeleted: boolean
}

function useLoadQuestionListData(opt: Partial<OptionType>) {
  const { isStar, isDeleted } = opt
  const [ searchParams ] = useSearchParams()
  const { data, loading, error, refresh} = useRequest(async () => {
    //获取搜索配置
    const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || ''
    const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY) || '') || 1
    const pageSize = parseInt(searchParams.get(LIST_PAGE_SIZE_PARAM_KEY) || '') || LIST_PAGE_SIZE
    const data = await getQuestionListService({ keyword, isStar, isDeleted, page, pageSize})
    return data
  }, {
    refreshDeps: [searchParams] //刷新的依赖项
  })
  return { data, loading, error, refresh }
}

export default useLoadQuestionListData